package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

import java.util.Scanner;

public class StudentService {
    private StudentDao studentDao = new StudentDao();

    public boolean removeStudent(String sid) {
        boolean flog = false;
        int index = isIndex(studentDao.findAllStudent(),sid);
        if (index != -1){
            studentDao.findAllStudent()[index] = null;
            flog = true;
        }
        return flog;
    }

    private int isIndex(Student[] stns, String sid) {
        int index = -1;
        for (int i = 0; i < stns.length; i++) {
            if (stns[i].getSid().equals(sid)){
                index = i;
                break;
            }
        }
        return index;
    }


    public Student[] findAllStudent() {
        Student[] stns = studentDao.findAllStudent();
        boolean flog = false;
        for (Student stn : stns) {
            if (stn != null) {
                flog = true;
                break;
            }
        }
        if (flog) {
            return stns;
        } else return null;
    }

    public boolean addStudent(Student stn) {
        return studentDao.addStudent(stn);
    }

    public boolean isExists(String sid) {
        Student[] stns = studentDao.findAllStudent();
        boolean flog = false;
        if (stns[0] == null) {
            return flog;
        } else {
            for (Student stn : stns) {
                if (stn != null && stn.getSid().equals(sid)) { //stns[i] = student对象 而stns[i].getSid是null执行了方法会直接报错
                    flog = true;
                    break;
                }
            }
            return flog;
        }
    }

    public boolean changeStudent(String sid) {
        boolean flog = false;
        Scanner sc = new Scanner(System.in);
        int index = isIndex(studentDao.findAllStudent(),sid);
        if (index != -1){
            System.out.println("请输入修改的姓名");
            String name = sc.next();
            System.out.println("请输入修改的年龄");
            String age = sc.next();
            System.out.println("请输入修改的生日");
            String birthday = sc.next();
            Student stn = new Student();
            stn.setName(name);
            stn.setAge(age);
            stn.setBirthday(birthday);
            studentDao.changeStudent(stn,index);
            flog = true;
        }
        return flog;

    }
}
